package com.example.excel.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.excel.R;

import androidx.annotation.NonNull;

public class LoadingDialog extends Dialog {

    private LoadingDialog mCustomProgressDialog = null;
    private Context mContext;

    public LoadingDialog(@NonNull Context context, String title) {
        super(context, R.style.Dialog);
        mContext = context;
        setContentView(R.layout.layout_custom_loading);

        TextView textView = findViewById(R.id.tv_progress);
        if(!TextUtils.isEmpty(title)){
            textView.setText(title);
            textView.setVisibility(View.VISIBLE);
        }
        this.setCanceledOnTouchOutside(false);
        this.setCancelable(false);
    }
    public LoadingDialog(@NonNull Context context) {
        super(context, R.style.Dialog);
        mContext = context;
        setContentView(R.layout.layout_custom_loading);
        this.setCanceledOnTouchOutside(false);
        this.setCancelable(false);
    }
    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mContext != null && mContext instanceof Activity) {
            if (mCustomProgressDialog != null && !((Activity) mContext).isFinishing()) {
                mCustomProgressDialog.dismiss();
            }
        }
        mCustomProgressDialog = null;
    }
}
